package com.antrain.his.controller;


import java.util.Map;

import javax.annotation.Resource;

import com.antrain.his.annotation.UserLoginToken;
import com.antrain.his.entity.Register;
import com.antrain.his.service.IRegisterService;
import com.antrain.his.utils.InitUtil;
import com.antrain.his.utils.Result;
import com.antrain.his.utils.ResultGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author antrain
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/registers")
public class RegisterController {
  @Resource
  private IRegisterService registerService;

  @GetMapping
  public Result getlist(@RequestParam Map<String, Object> param) {
      InitUtil.initPage(param);
      int num = Integer.parseInt(param.get("page").toString());
      int limit = Integer.parseInt(param.get("limit").toString());
      QueryWrapper<Register> wrapper = new QueryWrapper<>();
      InitUtil.initLike(param, wrapper, "CaseNumber");
      IPage<Register> page = new Page<>(num, limit);
      return ResultGenerator.getSuccessResult(registerService.getList(page, wrapper));
  }

  @GetMapping("/{id}")
  public Result getRegister(@PathVariable int id) {
      Register register = registerService.getById(id);
      if (register == null)
          return ResultGenerator.getFailResult("", "无挂号记录");
      return ResultGenerator.getSuccessResult(register);
  }

  @PostMapping()
  @UserLoginToken
  public Result save(@RequestBody Register register) {
      if (registerService.save(register))
          return ResultGenerator.getSuccessResult("", "添加成功");
      return ResultGenerator.getFailResult("", "添加失败");
  }

  @PutMapping("/{id}")
  @UserLoginToken
  public Result update(@RequestBody Register register, @PathVariable int id) {
      register.setId(id);
      if (registerService.updateById(register))
          return ResultGenerator.getSuccessResult("", "更新成功");
      return ResultGenerator.getFailResult("", "更新失败");
  }

  @DeleteMapping("/{id}")
  @UserLoginToken
  public Result del(@PathVariable int id) {
      if (registerService.removeById(id))
          return ResultGenerator.getSuccessResult("", "删除成功");
      return ResultGenerator.getFailResult("", "删除失败");
  }
}
